package com.cskaoyan.market.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @ClassName MyBatisUitl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/5/5 11:44
 * @Version V1.0
 **/
public class MyBatisUtil {

    private static SqlSessionFactory factory;


    static {
        ClassLoader classLoader = MyBatisUtil.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("mybatis.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSession getSession(){
        //建议不要使用openSession(true)自动提交，因为我们有需求要保证多条sql语句在一个事务内
        return factory.openSession();
    }
}
